#!/bin/bash

# HTTP API 演示脚本
echo "=== Learn Go Demo - HTTP API 演示 ==="
echo ""

# 检查 jq 是否安装
if ! command -v jq &> /dev/null; then
    echo "警告: 未安装 jq，输出可能不够美观"
    echo "建议安装: brew install jq (macOS) 或 apt-get install jq (Ubuntu)"
    echo ""
fi

USER_SERVICE_URL="http://localhost:8081/api/v1"
ORDER_SERVICE_URL="http://localhost:8082/api/v1"

# 等待服务启动
echo "等待服务启动..."
sleep 3

# 检查服务是否可用
echo "检查服务状态..."
if ! curl -s http://localhost:8081/health > /dev/null; then
    echo "❌ 用户服务未启动，请先运行: make user"
    exit 1
fi

if ! curl -s http://localhost:8082/health > /dev/null; then
    echo "❌ 订单服务未启动，请先运行: make order"
    exit 1
fi

echo "✅ 所有服务已启动"
echo ""

# 演示开始
echo "🚀 开始 HTTP API 演示..."
echo ""

# 1. 创建用户
echo "📝 1. 创建用户..."
USER_RESPONSE=$(curl -s -X POST "$USER_SERVICE_URL/users" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Alice Johnson",
    "email": "alice@example.com"
  }')

if command -v jq &> /dev/null; then
    echo "$USER_RESPONSE" | jq .
else
    echo "$USER_RESPONSE"
fi

USER_ID=$(echo "$USER_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
echo "✅ 用户创建成功，ID: $USER_ID"
echo ""

# 2. 获取用户信息
echo "👤 2. 获取用户信息..."
USER_INFO=$(curl -s "$USER_SERVICE_URL/users/$USER_ID")
if command -v jq &> /dev/null; then
    echo "$USER_INFO" | jq .
else
    echo "$USER_INFO"
fi
echo ""

# 3. 创建订单
echo "🛒 3. 创建订单..."
ORDER_RESPONSE=$(curl -s -X POST "$ORDER_SERVICE_URL/orders" \
  -H "Content-Type: application/json" \
  -d "{
    \"user_id\": $USER_ID,
    \"product\": \"MacBook Pro\",
    \"amount\": 2999.99
  }")

if command -v jq &> /dev/null; then
    echo "$ORDER_RESPONSE" | jq .
else
    echo "$ORDER_RESPONSE"
fi

ORDER_ID=$(echo "$ORDER_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
echo "✅ 订单创建成功，ID: $ORDER_ID"
echo ""

# 4. 获取订单信息
echo "📦 4. 获取订单信息..."
ORDER_INFO=$(curl -s "$ORDER_SERVICE_URL/orders/$ORDER_ID")
if command -v jq &> /dev/null; then
    echo "$ORDER_INFO" | jq .
else
    echo "$ORDER_INFO"
fi
echo ""

# 5. 创建更多订单
echo "🛍️ 5. 创建更多订单..."
curl -s -X POST "$ORDER_SERVICE_URL/orders" \
  -H "Content-Type: application/json" \
  -d "{
    \"user_id\": $USER_ID,
    \"product\": \"iPhone 15\",
    \"amount\": 999.99
  }" > /dev/null

curl -s -X POST "$ORDER_SERVICE_URL/orders" \
  -H "Content-Type: application/json" \
  -d "{
    \"user_id\": $USER_ID,
    \"product\": \"AirPods Pro\",
    \"amount\": 249.99
  }" > /dev/null

echo "✅ 已创建额外的订单"
echo ""

# 6. 获取用户的所有订单
echo "📋 6. 获取用户的所有订单..."
USER_ORDERS=$(curl -s "$ORDER_SERVICE_URL/orders/user/$USER_ID")
if command -v jq &> /dev/null; then
    echo "$USER_ORDERS" | jq .
else
    echo "$USER_ORDERS"
fi
echo ""

# 7. 更新订单状态
echo "✏️ 7. 更新订单状态..."
UPDATE_RESPONSE=$(curl -s -X PUT "$ORDER_SERVICE_URL/orders/$ORDER_ID/status" \
  -H "Content-Type: application/json" \
  -d '{"status": "completed"}')

if command -v jq &> /dev/null; then
    echo "$UPDATE_RESPONSE" | jq .
else
    echo "$UPDATE_RESPONSE"
fi
echo ""

# 8. 确认状态更新
echo "🔍 8. 确认订单状态更新..."
UPDATED_ORDER=$(curl -s "$ORDER_SERVICE_URL/orders/$ORDER_ID")
if command -v jq &> /dev/null; then
    echo "$UPDATED_ORDER" | jq .
else
    echo "$UPDATED_ORDER"
fi
echo ""

echo "🎉 HTTP API 演示完成！"
echo ""
echo "💡 提示："
echo "  - 用户服务 HTTP API: http://localhost:8081/api/v1"
echo "  - 订单服务 HTTP API: http://localhost:8082/api/v1"
echo "  - 健康检查: http://localhost:8081/health 和 http://localhost:8082/health"
echo "  - 详细 API 文档: docs/HTTP_API.md"
